package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.service.IUserService;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.query.UserQuery;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: UserController
 * Package: com.itheima.mp.controller
 * Description:
 *
 * @Author 那啥啥
 * @Create 2024/1/6  20:00
 * @Version 1.0
 */
@Api(tags = "用户接口管理")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor  //需要用到的属性的构造参数去构造，，需要构造的加上final  springboot就会自动注入
public class UserController {
//    @Autowired
//    private IUserService userService;//springboot不推荐
   /* private IUserService userService;
//springboot推荐
    public UserController(IUserService userService) {
        this.userService = userService;
    }*/

private final IUserService userService;
    @ApiOperation("新增用户接口")
    @PostMapping
    public void saveUser(@RequestBody UserFormDTO userFormDTO){
        //把DTO拷贝到PO
        User user = BeanUtil.copyProperties(userFormDTO, User.class);//hutool包下
        //新增

        userService.save(user);
    }

    @ApiOperation("删除用户接口")
    @DeleteMapping("/{id}")
    public void deleteById(@ApiParam("用户id") @PathVariable("id") Long id){
        userService.removeById(id);
    }

    @ApiOperation("根据id查询用户接口")
    @GetMapping("/{id}")
    public UserVO queryUserById(@ApiParam("用户id") @PathVariable("id") Long id){
 /*       User user = userService.getById(id);

//        把po拷贝到vo
        return BeanUtil.copyProperties(user ,UserVO.class);*/

        return userService.queryUsersAndAddressById(id);
    }

    @ApiOperation("根据id批量查询用户接口")
    @GetMapping
    public List<UserVO> queryUserByBatchId(@ApiParam("用户ids集合") @RequestParam("ids") List<Long> ids){
    /*    //查询po
        List<User> users = userService.listByIds(ids);

        //拷贝到vo
        return BeanUtil.copyToList(users,UserVO.class);*/

        return  userService.queryUserByIds(ids);
    }
    @ApiOperation("根据id扣减余额接口")
    @PutMapping("/{id}/deduction/{money}")
    public void deductBalance(@ApiParam("用户id") @PathVariable("id") Long id,
                               @ApiParam("扣减的金额") @PathVariable("money") Integer money){
    userService.deductBalance(id,money);
    }

    @ApiOperation("根据复杂条件查询用户接口")
    @GetMapping("/list")
    public List<UserVO> queryUsers(UserQuery query){
        //查询po
        List<User> users = userService.queryUsers(query.getName(), query.getMinBalance(),
                query.getMaxBalance(), query.getStatus());
        //拷贝到vo
        return BeanUtil.copyToList(users,UserVO.class);
    }



}
